<template>
	<view class="cu-modal bottom-modal" :class="user.showLogin?'show':''" @touchmove.stop.prevent  @tap="setLogin(false)">
		<view class="cu-dialog cu-dialog-radius">
			<view class="padding-lg">
				<view class="dir-site" @tap="setLogin(false)">
					<text class="cuIcon-close text-gray"></text>
				</view>
				<view class="header">
					<image class="header-img" src="/static/img/avatar.png" mode="scaleToFill"></image>
				</view>
				<!-- #ifdef MP-WEIXIN -->
				<button class="cu-btn bg-green round lg margin-top-xl" style="width: 100%;"
					 open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
					一键登录
				</button>
				<!-- #endif -->
				<!-- #ifdef H5 -->
				<button class="cu-btn bg-green round lg margin-top-xl" style="width: 100%;"
					 @tap="login">
					一键登录
				</button>
				<!-- #endif -->
			</view>
		</view>
	</view>
</template>

<script>
	import { setToken, wxLogin, getProfile } from '@/common/api/auth.js'
	import { mapState, mapActions } from 'vuex';
	export default{
		computed:{
			...mapState(['user']),
		},
		methods:{
			...mapActions({
				userLogin: 'user/login',
				setLogin: 'user/setLogin'
			}),
			getPhoneNumber(e) {
				if (e.detail&&e.detail.errMsg!='getPhoneNumber:ok')
					return
				const that = this
				that.$tools.showLoad('正在登录')
				wx.login({
					success: res => {
						wxLogin({
							code: res.code,
							encryptedData: e.detail.encryptedData,
							iv: e.detail.iv,
						}).then(res => {
							uni.hideLoading()
							if (res&&res.code!==0) {
								that.userLogin(res.data)
								if(res.data.is_broker) {
									uni.setStorageSync('isBroker', res.data.is_broker)
									uni.setStorageSync('brokerId', res.data.is_broker)
								}
								that.$emit('success');
							}
						})
					}
				})
			},
			login() {
				const that = this
				that.$tools.showLoad('正在登录')
				setToken("254c26ec-de32-42a3-805b-1fdaaf3025a0")
				getProfile().then(res => {
					uni.hideLoading()
					if (res&&res.code!==0) {
						that.userLogin(res.data)
						if(res.data.is_broker) {
							uni.setStorageSync('isBroker', res.data.is_broker)
							uni.setStorageSync('brokerId', res.data.is_broker)
						}
						that.$emit('success');
					}
				}) 
			}
		}
	}
</script>

<style>
	.cu-dialog-radius{
		border-top-left-radius: 20rpx !important;
		border-top-right-radius: 20rpx !important;
	}
	.dir-site{
		position: absolute;
		right: 30rpx;
		top: 30rpx;
		font-size: 36rpx;
		z-index: 10;
	}
	.header {
		width: 190rpx;
		height: 190rpx;
		border: 2px solid #fff;
		margin: 0rpx auto 0;
		border-radius: 50%;
		overflow: hidden;
		box-shadow: 1px 0px 5px rgba(50, 50, 50, 0.3);
	}
	.header-img {
		width: 200rpx;
		height: 200rpx;
	}
</style>
